package chalmers.game.subterranean.ui.control;

import chalmers.game.subterranean.model.player.IPlayer;
import chalmers.game.subterranean.util.Direction;
import chalmers.game.subterranean.util.options.Keybindings;
import javafx.event.EventHandler;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

/**
 * 
 * @author Alex Evert
 */
public class KeyPressedHandler implements EventHandler<KeyEvent> {

  private final IPlayer player;

  public KeyPressedHandler(IPlayer player) {
    this.player = player;
  }

  @Override
  public void handle(KeyEvent t) {
    if (t.getCode().equals(Keybindings.JUMP_KEY)) {
      player.jump();

    } else if (t.getCode().equals(Keybindings.ACTION_KEY)) {
      player.action();

    } else if (t.getCode().equals(Keybindings.TOGGLE_KEY)) {
      player.equipNextItem();

    } else if (t.getCode().equals(Keybindings.USE_KEY)) {
      player.use();

    } else if (t.getCode().equals(Keybindings.UP_KEY)) {
      player.look(Direction.NORTH);
      player.run(Direction.NORTH);

    } else if (t.getCode().equals(Keybindings.DOWN_KEY)) {
      player.look(Direction.SOUTH);
      player.run(Direction.SOUTH);

    } else if (t.getCode().equals(Keybindings.LEFT_KEY)) {
      player.look(Direction.WEST);
      player.run(Direction.WEST);

    } else if (t.getCode().equals(Keybindings.RIGHT_KEY)) {
      player.look(Direction.EAST);
      player.run(Direction.EAST);

    } else if (t.getCode().equals(KeyCode.ESCAPE)) {
      System.exit(0);
    }
  }
}
